﻿using UtilPublic.Mysql;
using UtilPublic.ServerLoger;
using System;
using System.Diagnostics;
using System.Threading;
using UtilLib;
using ServerBase.Config;
using System.Collections.Concurrent;
using UtilPublic.Const;
using ServerBase.LunarSocket;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ServerBase.Service
{
    /// <summary>
    /// 服务器信息
    /// </summary>
    public static class BaseServerInfo
    {
        // 服务器类型
        public static EServerType ServerType = EServerType.游戏;

        // 服务器配置ID
        public static int ConfigId { get { return PublicConst.ServerConfigId; } set { PublicConst.ServerConfigId = value; } }
        // 服务器配置目录
        public static string ServerConfigDir = "Base";
        // 服务器配置
        public static ConfigServerGame Cfg = new ConfigServerGame();
        //服务器运营平台
        public static EServerPlatform ServerPlatform => (EServerPlatform)(Cfg.ServerPlatform);
        // 服务器平台配置
        public static ConfigServerPlatform CfgPlatform = new ConfigServerPlatform();

        
        #region 判断本地IP地址是否符合配置表里的对外IP
        //非本机调试 判断本地IP地址是否符合配置表里的对外IP 符合才能启动
        public static bool 判断本地IP地址是否符合配置表里的对外IP = false;
        // 机器本地ip地址 配置在 zzz.txt
        public static string LocalIp = "127.0.0.1"; 
        #endregion


        // 服务器运行模式
        public static EServerRunMode ServerRunMode => (EServerRunMode)(Cfg.ServerRunMode);
        // 服务器IP
        public static string ServerIp => Cfg.ServerIp;
        // 服务器默认端口
        public static int ServerPort => Cfg.ServerPort;
        // 当前语言版本
        static public ELanguage Language => (ELanguage)(Cfg.Language);
        // 开服时间
        public static DateTime ServerTimeOpen => (Cfg.TimeOpen.ToDateTime());
        // 服务器默认区名
        public static string ServerZone => $"{Cfg.ServerName} {ConfigId}";

        
        //数据库状态
        public static bool MysqlState = false;
        //限制登录
        public static bool RestrictLogin = false;

        public static bool ServerRunModeIs外网 => (ServerRunMode == EServerRunMode.外网测试 || ServerRunMode == EServerRunMode.正式发布);

        // 服务器启动时间
        public static DateTime ServerStartupTime = DateTime.Now;

        // 默认心跳时间
        public static int ServerHeartbeat = 10;

        #region 其他端口
        //其他端口
        public static int PortHttpServer => ServerPort + 1;//HttpServer
        #endregion

        // 主线程数据库连接
        public static DbMysql DbMysqlMain = null;

        //服务器永久线程列表
        public static List<Task> ListTasks = new List<Task>();

        #region 服务器进程
        // 当前进程
        public static Process ServerCurrentProcess { get { return Process.GetCurrentProcess(); } }

        //进程ID
        public static int ServerCurrentProcessID { get { return ServerCurrentProcess.Id; } }

        //CPU使用率
        public static float ServerCurrentProcessCpu
        {
            get
            {
                return ((float)(ServerCurrentProcess.UserProcessorTime.TotalMilliseconds / ServerCurrentProcess.TotalProcessorTime.TotalMilliseconds));
            }
        }

        //内存
        public static long ServerCurrentProcessMemory { get { return ServerCurrentProcess.PeakWorkingSet64; } }

        //线程数
        public static int ServerCurrentProcessThreads { get { return ServerCurrentProcess.Threads.Count; } }
        #endregion


        // 设置服务器运行模式
        public static void SetRunMode()
        {
            loger.Info("服务器初始化成功！");

            Util.Show($"当前运行的是 {ServerRunMode} 版本");

            loger.临时调试开关 = ServerRunMode == EServerRunMode.本机调试;
            if (ServerRunModeIs外网)
            {
                loger.ChangeLevelRelease();
            }
        }

    }



}
